
//liuweibo add start 20120326
package com.android.matmanual;



import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ProgressBar;


public class LCDTest extends BaseActivity 
{
	
	

	ProgressBar mProgressBar;
	
	LcdChangeHandler mLcdChangeHandler;
	
	float mOldBrightness;
	int mProgress = 0;
	
	WindowManager.LayoutParams mLayoutParams;
	
	
	private static final int LCD_INCREASE_CHANGE = 1;  
	private static final int LCD_DECREASE_CHANGE = 2;  
	
    private static final int MINIMUM_BACKLIGHT = /*android.os.PowerManager.BRIGHTNESS_DIM */20+ 10; //lifei
    private static final int MAXIMUM_BACKLIGHT = android.os.PowerManager.BRIGHTNESS_ON;

    WakeLock mWakeLock;

    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
     
		setContentView(R.layout.lcd_test);
		setTitle(R.string.lcd_title);
	
	    PowerManager pm =
		           (PowerManager) getSystemService(POWER_SERVICE); 
		    mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, 
		          "XYTEST"); 
		    mWakeLock.acquire(); 	
		    
	
		
		
		mProgressBar= (ProgressBar) findViewById(R.id.progressbar);	
		mProgressBar.setMax(MAXIMUM_BACKLIGHT - MINIMUM_BACKLIGHT);
		mProgressBar.setProgress(mProgress);
       
		mLcdChangeHandler = new LcdChangeHandler();
		

        mLayoutParams = getWindow().getAttributes();
        mOldBrightness = mLayoutParams.screenBrightness;
        
		mProgress = 0;
		mProgressBar.setProgress(mProgress);
		mLcdChangeHandler.sendEmptyMessageDelayed(LCD_INCREASE_CHANGE,5);
	}



	private class LcdChangeHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			  switch (msg.what) {
			  
              case LCD_INCREASE_CHANGE: 
            	  setBrightness(mProgress + MINIMUM_BACKLIGHT);
            	  mProgressBar.setProgress(mProgress);
            	  mProgress = mProgress+4;
            	  if(mProgress>(MAXIMUM_BACKLIGHT-MINIMUM_BACKLIGHT))
            	  {
            		  mLcdChangeHandler.sendEmptyMessageDelayed(LCD_DECREASE_CHANGE,5);
            	  }
            	  else
            	  {
            		  mLcdChangeHandler.sendEmptyMessageDelayed(LCD_INCREASE_CHANGE,5);
            	  }
            	  
            	  break;
              case LCD_DECREASE_CHANGE:	 
            	  setBrightness(mProgress + MINIMUM_BACKLIGHT);
            	  mProgressBar.setProgress(mProgress);
            	  mProgress = mProgress-4;
            	  if(mProgress<0)
            	  {
            		  mLcdChangeHandler.sendEmptyMessageDelayed(LCD_INCREASE_CHANGE,5);
            	  }
            	  else
            	  {
            		  mLcdChangeHandler.sendEmptyMessageDelayed(LCD_DECREASE_CHANGE,5);
            	  }
            	  
            	  break;
          }
		}
    }
    
    private void setBrightness(int brightness) {
    	
    	mLayoutParams.screenBrightness = (float)brightness/MAXIMUM_BACKLIGHT;
        getWindow().setAttributes(mLayoutParams);
      
    }

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
        finish();
		super.onStop();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
    	mLayoutParams.screenBrightness = mOldBrightness;               
        getWindow().setAttributes(mLayoutParams);
        mLcdChangeHandler.removeMessages(LCD_INCREASE_CHANGE);
        mLcdChangeHandler.removeMessages(LCD_DECREASE_CHANGE);
		if(mWakeLock!=null)
		{
			mWakeLock.release(); 
			mWakeLock = null;
		}
	}
		

	@Override
	public void onAttachedToWindow() 
	{
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
		this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
	}
    
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		int result = TestControl.GetKeyEvent(keyCode);
		if (result != TestControl.DO_NOTHING) {
			setResult(result);
			finish();
	    }
	    return true;
	}
}
//liuweibo end
		 